package com.shkco.org.controller;

import com.shkco.basic.util.PageList;
import com.shkco.basic.util.AjaxResult;
import com.shkco.org.domain.Employee;
import com.shkco.org.query.EmployeeQuery;
import com.shkco.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询员工信息" )
    public Employee findById(@PathVariable("id")Long id){
        return employeeService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有员工信息" )
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除员工信息" )
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "新增或修改员工信息" )
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    @ApiOperation(value = "批量删除" )
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }



    @GetMapping("/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id")Long shopId){
        return employeeService.findByShopId(shopId);
    }
}
